import Taro, { Component } from "@tarojs/taro";
import { connect } from "@tarojs/redux";
import { format, addDays } from 'date-fns'
import _isEmpty from 'lodash/isEmpty'
import { View, Text, Image } from '@tarojs/components';
import { getPublicData } from '@/actions/common';
import { placeOrder, getOrderPrice, getMatchAirs, placeConfirmOrder, getCityAirList } from '@/actions/order'
import { getDaysList, getCommonHoursList, getMinutesList, getDayIndex, getSelectFromArray} from '@/utils'
import { agingData, airReceiveMethodList } from '@/utils/constants'

import AddressItem from '@/components/AddressItem'
import ForbidGoods from '@/components/ForbidGoods'
import RowItem from "@/components/RowItem";
import BaseButton from "@/components/BaseButton";
import AddressLine from '@/components/AddressLine'
import RadioItem from "@/components/RadioItem"
import GTagList from "@/components/GTagList"
import { ModalBottom, ModalMid } from "@/components/CustomModal";

import './index.scss';

// TODO: 已填写数据，跳转对应页面应显示 已填写的数据！！！！

const days = getDaysList(2)
const hours = getCommonHoursList()
const mins = getMinutesList()
@connect(({ common, order}) => ({
  common, 
  order
}))
class OrderPage extends Component {

  config = {
    navigationBarTitleText: '发货下单'
  };

  constructor(props) {
    super(props);

    this.state = {
      trafficList: [],
      traModal: false,      // 运输类型切换Modal
      traType: '',
      tempType: ''
    };
    this.timer = 0;
  }

  componentDidShow(){
    const type = this.$router.params.type
    this.setState({ 
      traType: type,
      tempType: type,
    })
  }

  componentDidMount(){
    const { dispatch } = this.props
    dispatch(getPublicData({ type: 'traffic_type' }, (data) => {
      this.setState({ trafficList: data || []})
    }))
  }

  render() {
    const { traModal, trafficList, traType, tempType} = this.state;
  
    return (
      <View className="air-order-page">
      

        {/* 运输类型modal */}
        <ModalBottom 
          title='选择运输类型'
          isOpened={traModal}
          onClose={() => { this.setState({ traModal: false }) }}
          renderFooter={(<BaseButton cls='tra-btn' text='确定' center type='primary' width='90%' onClick={() => this.handleConfirmTraffic()}/>)}
        >
          <GTagList data={trafficList} value={tempType} onClick={(td) => this.handleSelectTraffic(td)} />
        </ModalBottom>
      </View>
    )
  }

  showSwitchTraffic = (type) => {
    this.setState({ 
      traType: type,
      traModal: true,
    })
  }

  /** 运输类型切换 */
  handleSelectTraffic = (td) => {
    this.setState({
      tempType: td.code
    })
  }

  handleConfirmTraffic = () => {
    const { tempType } = this.state
    this.setState({ 
      traModal: false,
      traType: tempType
    })
    Taro.redirectTo({ url: `/pages/order/index?type=${tempType}`})
  }

  jump2page = (path) => {
    const { preOrder } = this.state
    switch(path){
      case 'forbid':
        Taro.navigateTo({ url: `/pages/rules/${path}?type=${this.$router.params.type}`})
        break;
      default: 
        Taro.navigateTo({ url: `/pages/${path}/index?rId=${preOrder.orderId}$type=${this.$router.params.type}`})
        break;
    }
  }

  /**再下一单 */
  toAgainPlaceOrder = () => {
    console.log("再下一单")
  }


  /** 明细显示 */
  handleOrderDetailModal = (flag) => {
    this.setState({ detailModal: !!flag })
  }

  /** 显示条件页面 */
  showConditionPage = (path) => {
    if(path === 'time'){
      this.setState({ timeModal: true })
    }else{
      const url = `/pages/order/${path}/index?type=${this.$router.params.type}`
      Taro.navigateTo({ url })
    }
  }

  inputBlur = () => {
		this.timer = setTimeout(() => {
			window.scrollTo(0,0);
		}, 0);
	}
	inputFocus = () => {
		clearTimeout(this.timer);
	}
}

export default OrderPage;